package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.questionanalysis.NoQuestionAnalysisException;
import com.xinzhitong.www.exception.questionanalysis.TypeErrException;
import com.xinzhitong.www.exception.questionerr.QuestionAnswerIsNullException;
import com.xinzhitong.www.exception.test.EmptyQuestionOfTestException;
import com.xinzhitong.www.exception.test.EmptyTestOfTheCredentialException;
import com.xinzhitong.www.exception.test.NullTestQuestionException;
import com.xinzhitong.www.pojo.User;
import com.xinzhitong.www.service.QuestionBankService;
import com.xinzhitong.www.service.TestService;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 测试控制器
 *
 * @author 徐鑫 魏洛淳 钟伟良
 */
@CrossOrigin
@RestController
@RequestMapping("test")
public class TestController {

    @Resource
    TestService testService;

    @Resource
    QuestionBankService questionBankService;

    /**
     * 获取指定证书下的随机一套测试的测试基本信息
     *
     * @param credentialId 证书id
     * @return 测试基本信息
     */
    @RequestMapping(value = "randomTestInfoOfCredential", method = RequestMethod.GET)
    public Message randomTestInfoOfCredential(@RequestParam("credentialId") Integer credentialId) {
        String msg;
        try {
            return Message.createSuc(testService.getTestInfo(credentialId));
        } catch (EmptyTestOfTheCredentialException e) {
            e.printStackTrace();
            msg = "此证书下暂无测试，正在建设中";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取指定测试的题目（分块）
     *
     * @param testId 测试id
     * @return 题目块
     */
    @RequestMapping(value = "theBlockOfTest", method = RequestMethod.GET)
    public Message theBlockOfTest(@RequestParam("testId") Integer testId) {
        String msg;
        try {
            return Message.createSuc(testService.getQuestionBlocksOfTest(testId));
        } catch (EmptyQuestionOfTestException e) {
            e.printStackTrace();
            msg = "此测试下暂无题目，正在建设中";
        }
        return Message.createErr(msg);
    }

    /**
     * 测试批卷
     *
     * @param questions 问题们
     * @return 测试批卷结果和总分
     */
    @RequestMapping(value = "readTest", method = RequestMethod.POST)
    public Message readTest(
            @RequestBody Map<String, List<Map<String, Object>>> questions
    ) {
        String msg;
        try {
            return Message.createSuc(testService.readTest(questions));
        } catch (QuestionAnswerIsNullException e) {
            e.printStackTrace();
            msg = "暂无题目答案";
        }
        return Message.createErr(msg);
    }

    /**
     * 在测试中获取题面
     *
     * @param index        索引
     * @param questionType 类型
     * @return 题面
     */
    @RequestMapping(value = "testQuestion", method = RequestMethod.GET)
    public Message readTest(
            @RequestParam("index") Integer index,
            @RequestParam("questionType") String questionType
    ) {
        String msg;
        try {
            return Message.createSuc(testService.getTestQuestion(index, questionType));
        } catch (NullTestQuestionException e) {
            e.printStackTrace();
            msg = "暂无题目";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取指定题目的正确答案
     *
     * @param questionType 题目类型
     * @param index        题目索引
     * @return 正确答案
     */
    @RequestMapping(value = "theCorrectAnswerOfTheQuestion", method = RequestMethod.GET)
    public Message theCorrectAnswerOfTheQuestion(
            @RequestParam("questionType") String questionType,
            @RequestParam("index") Integer index
    ) {
        String msg;
        try {
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipal();
            Integer id = testService.getQuestionIdOfTest(user.getUsername(), questionType, index);
            return Message.createSuc(questionBankService.getQuestionAnalysis(id, questionType));
        } catch (TypeErrException e) {
            e.printStackTrace();
            msg = "题目号与类型不符";
        } catch (NoQuestionAnalysisException e) {
            e.printStackTrace();
            msg = "此题目没有答案";
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            msg = "题目不存在";
        }
        return Message.createErr(msg);
    }

}
